﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HtmlExtractor.XPathOutput
{
    public enum XpathType
    {
        Element,
        Attribute
    }
    public class XPathOutputPathParser
    {
        public readonly XpathType XpathType;
        public readonly string LastSegment;
        public readonly string XPath;
        public readonly string ElementSegment;
        public readonly string AttributeSegment;
        public readonly string CompositeXPath;
        public XPathOutputPathParser(string outputPath)
        {
            string[] s = outputPath.Split(',');
            if (s.Length == 2)
            {
                CompositeXPath = s.Last();
                XPath = s.First();
              
            }
            else if (s.Length == 1)
            {
                CompositeXPath = null;
                XPath = s.First();
            }
            else
                throw new NotSupportedException(outputPath+"格式不正确");
            LastSegment = XPath.Split('/').Last();
            
            if (LastSegment.Contains('@'))
            {
                XpathType = XpathType.Attribute;
                AttributeSegment = LastSegment;
                ElementSegment = XPath.Replace(LastSegment, "");
            }
            else
            {
                XpathType = XpathType.Element;
                ElementSegment = XPath;
            }
            ElementSegment=ElementSegment.TrimEnd('/');
        }
       
    }
}
